/*
*  Copyright 2019-2020 Zheng Jie
*
*  Licensed under the Apache License, Version 2.0 (the "License");
*  you may not use this file except in compliance with the License.
*  You may obtain a copy of the License at
*
*  http://www.apache.org/licenses/LICENSE-2.0
*
*  Unless required by applicable law or agreed to in writing, software
*  distributed under the License is distributed on an "AS IS" BASIS,
*  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*  See the License for the specific language governing permissions and
*  limitations under the License.
*/
package me.zhengjie.modules.cspimp.psp.service.impl;

import me.zhengjie.modules.cspimp.psp.domain.Accident;
import me.zhengjie.modules.cspimp.psp.domain.PollingRude;
import me.zhengjie.modules.cspimp.psp.service.dto.AccidentDto;
import me.zhengjie.modules.system.domain.AreaManage;
import me.zhengjie.modules.system.domain.User;
import me.zhengjie.modules.system.repository.UserRepository;
import me.zhengjie.utils.ValidationUtil;
import me.zhengjie.utils.FileUtil;
import lombok.RequiredArgsConstructor;
import me.zhengjie.modules.cspimp.psp.repository.PollingRudeRepository;
import me.zhengjie.modules.cspimp.psp.service.PollingRudeService;
import me.zhengjie.modules.cspimp.psp.service.dto.PollingRudeDto;
import me.zhengjie.modules.cspimp.psp.service.dto.PollingRudeQueryCriteria;
import me.zhengjie.modules.cspimp.psp.service.mapstruct.PollingRudeMapper;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import me.zhengjie.utils.PageUtil;
import me.zhengjie.utils.QueryHelp;
import org.springframework.util.CollectionUtils;

import java.util.List;
import java.util.Map;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.LinkedHashMap;

/**
* @website https://docs.auauz.net
* @description 服务实现
* @author zmm
* @date 2020-05-30
**/
@Service
@RequiredArgsConstructor
@Transactional(propagation = Propagation.SUPPORTS, readOnly = true, rollbackFor = Exception.class)
public class PollingRudeServiceImpl implements PollingRudeService {

    private final PollingRudeRepository pollingRudeRepository;
    private final PollingRudeMapper pollingRudeMapper;
    private final UserRepository userRepository;

    @Override
    public Map<String,Object> queryAll(PollingRudeQueryCriteria criteria, Pageable pageable){
        List<PollingRude> page = pollingRudeRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder));
        List<PollingRudeDto> pollingRudeDtoList=new ArrayList<>();
        if (!CollectionUtils.isEmpty(page)) {
            for (PollingRude pollingRude : page) {
                PollingRudeDto pollingRudeDto = new PollingRudeDto();
                BeanUtils.copyProperties(pollingRude, pollingRudeDto);
                User user = userRepository.findById(pollingRude.getPollingUserId()).orElse(null);
                if (null != user) {
                    pollingRudeDto.setUsername(user.getUsername());
                }
                pollingRudeDtoList.add(pollingRudeDto);
            }
        }
        return PageUtil.toPage(PageUtil.listPage(pollingRudeDtoList, pageable));
    }

    @Override
    public List<PollingRudeDto> queryAll(PollingRudeQueryCriteria criteria){
        return pollingRudeMapper.toDto(pollingRudeRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder)));
    }

    @Override
    public PollingRudeDto findById(Long id) {
        PollingRude pollingRude = pollingRudeRepository.findById(id).orElseGet(PollingRude::new);
        ValidationUtil.isNull(pollingRude.getId(),"PollingRude","id",id);
        return pollingRudeMapper.toDto(pollingRude);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public PollingRudeDto create(PollingRude resources) {
        resources.setCid(0l);
        return pollingRudeMapper.toDto(pollingRudeRepository.save(resources));
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void update(PollingRude resources) {
        PollingRude pollingRude = pollingRudeRepository.findById(resources.getId()).orElseGet(PollingRude::new);
        ValidationUtil.isNull( pollingRude.getId(),"PollingRude","id",resources.getId());
        pollingRude.copy(resources);
        pollingRudeRepository.save(pollingRude);
    }

    @Override
    public void deleteAll(Long[] ids) {
        for (Long id : ids) {
            pollingRudeRepository.deleteById(id);
        }
    }

    @Override
    public void download(List<PollingRudeDto> all, HttpServletResponse response) throws IOException {
        List<Map<String, Object>> list = new ArrayList<>();
        for (PollingRudeDto pollingRude : all) {
            Map<String,Object> map = new LinkedHashMap<>();
            map.put("创建者", pollingRude.getCreateBy());
            map.put("更新者", pollingRude.getUpdateBy());
            map.put("创建日期", pollingRude.getCreateTime());
            map.put("更新时间", pollingRude.getUpdateTime());
            map.put("巡检名称", pollingRude.getName());
            map.put("巡检周期", pollingRude.getCycle());
            map.put("巡检时间", pollingRude.getPollingDate());
            map.put("巡检人员", pollingRude.getPollingUserId());
            list.add(map);
        }
        FileUtil.downloadExcel(list, response);
    }
}